1. Field of the Invention
Embodiments of the present invention generally relate to scripting programming languages and, more particularly, to a method and apparatus for dependency tracking in JavaScript.
2. Description of the Related Art
Javascript is a scripting language for creating web pages with dynamic functionality for use in writing relatively simple applications such as web forms. Forms are used to pass data to a server. A form can contain input elements such as text fields, check boxes, radio-buttons, submit buttons and the like. Online shopping is an example of a form experience. There exist multiple form fields for an online shopping cart. For example, it may contain input elements for a plurality of sale items such as a quantity field for each item. It also typically contains a radio-button for selecting a desired shipping method. In addition to these input fields, the shopping cart form contains calculated fields such as subtotal, tax, shipping charge, and grand total. A change in the quantity of an item results in a change to the subtotal of an order, which results in a change to the tax, shipping and grand total. The subtotal is said to depend from the quantity, while the tax, shipping and grand total are said to depend from the subtotal. When an input field, such as quantity or shipping method is changed, ideally, all calculated fields that depend on the changed input field update automatically. For this to happen, the calculated fields must know on what input fields they depend. Each calculated field must know when one of the input fields from which it depends is changed, so the value of the field can be recalculated.
A number of methods currently exist to ensure that the dependent fields are correctly recalculated. In one such method, any time any input field is changed, all quantities within the form are recalculated. This however creates performance issues due to unnecessary recalculating of unaffected fields. In another method, when a quantity is first calculated, every object that was referenced during the calculation is noted and stored. When the object updates, the object sends out a notification that the value of the object has updated. This is called an observer pattern. The recalculated object, referred to as a dependent object knows what it is dependent on and establishes a listening relationship with the object from which it depends. The dependent object listens for updates and if the dependent object sees a change in the object from which it depends, the dependent object recalculates itself. However, such tracking algorithms impose awkward syntax on the expression grammar which is used to track these dependencies. Conventional tracking algorithms use function accessors for all properties, for example this.price( )*this.quantity( ). These complicated expressions are difficult for a novice forms author who is familiar with expressing calculated fields using simple expressions such as price*quantity and who wants changes in input fields to be automatically reflected in dependent calculated fields.
Therefore, there is a need for an improved method and apparatus for dependency tracking in JavaScript.